﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarioArena.World;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MarioArena.Animation
{
    public class Explosion
    {
        public bool HasFinished { get; set; }

        private Vector2 mPosition;
        private int mWidth;
        private int mHeight;
        private int mDuration;
        private long mStartTime;

        public enum Type
        {
            Small, Death
        }

        SpriteAnimation mAnimation;

        /// <summary>
        /// Constructs a new explosion.
        /// </summary>
        /// <param name="cameraPos">The (camera)position of the center of the explosion.</param>
        /// <param name="explosionType">The type of explosion.</param>
        public Explosion(Vector2 cameraPos, Type explosionType)
        {
            this.HasFinished = false;
            this.mPosition = cameraPos;

            if (explosionType == Type.Small)
            {
                Texture2D texture = GameManager.Instance.ExplosionSmallSpritesheet;
                int frames = 25;
                int fps = 80;
                this.mAnimation = new SpriteAnimation(texture, frames);
                this.mAnimation.framesPerSecond = fps;
                this.mWidth = texture.Width / frames;
                this.mHeight = texture.Height;
                this.mDuration = 1000 / (fps / frames);
            }
            else if (explosionType == Type.Death)
            {
                Texture2D texture = GameManager.Instance.ExplosionDeathSpritesheet;
                int frames = 22;
                int fps = 30;
                this.mAnimation = new SpriteAnimation(texture, frames);
                this.mAnimation.framesPerSecond = fps;
                this.mWidth = texture.Width / frames;
                this.mHeight = texture.Height;
                this.mDuration = 1000 / (fps / frames);
            }

            this.mAnimation.isLooping = false;

            this.mStartTime = DateTime.Now.Ticks / 10000;
        }

        /// <summary>
        /// Updates the explosion.
        /// </summary>
        public void Update(GameTime gameTime)
        {
            if (!this.HasFinished)
            {
                long currentTime = DateTime.Now.Ticks / 10000;
                if (currentTime - this.mStartTime > this.mDuration)
                    this.HasFinished = true;

                this.mAnimation.Update(gameTime);
            }
        }

        /// <summary>
        /// Draws the explosion.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            // Draw the explosion centered at the position
            //this.mAnimation.Draw(spriteBatch,this.mPosition - (new Vector2(this.mWidth, this.mHeight) / 2));

            if (!this.HasFinished)
            {
                this.mAnimation.Draw(spriteBatch,
                    new Vector2(GameManager.Instance.HalfScreenWidth, GameManager.Instance.HalfScreenHeight)
                    + this.mPosition - (new Vector2(this.mWidth, this.mHeight) / 2)
                    - new Vector2(Camera.X, Camera.Y)
                    );
            }
        }
    }
}
